Excel Sheet Column Number

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

  1. A -> 1
  2. B -> 2
  3. C -> 3
  4. ...
  5. Z -> 26
  6. AA -> 27
  7. AB -> 28

Solution:

  1. public class Solution {
  2. public int titleToNumber(String s) {
  3. int num = 0;
  4. for (int i = 0; i < s.length(); i++) {
  5. num = num * 26 + (int)(s.charAt(i) - 'A') + 1;
  6. }
  7. return num;
  8. }
  9. }